import './plugin'
import {Observer} from '../core/util'

// 初始内容
const initialContent = UEDITOR_CONFIG.autotypeset.indent ? `<p style="text-indent: ${UEDITOR_CONFIG.autotypeset.indentValue};">${UE.browser.ie ? '' : '<br>'}</p>` : `<p>${UE.browser.ie ? '' : '<br>'}</p>`;
// const initialContent = `<p>${UE.browser.ie ? '' : '<br>'}</p>`;

let saveTimer;

function initEditor(content) {
    let editor = UE.getEditor('editor', {
        initialStyle: 'body{overflow:hidden!important;}',
        initialContent: content || "",
        focus: true,
        imageActionName: window.globalVar.baseURL + "/api/Editor/FileUpload?t=image", // 图片上传路径
        // fileActionName: window.globalVar.baseURL + "/api/Editor/AttachFileUpload?t=file", // 文件上传路径
        catchRemoteImageEnable: false, // 关闭抓取远程图片的插件，如果开启，需要配置imageUrl，serverUrl，catcherActionName
        imageFieldName: 'fileValue',
        fileFieldName: 'fileValue',
        fileMaxSize: 512000000, // 附件上传的最大容量
        fileUrlPrefix: "", // 文件访问路径前缀
        deleteTargets: { // contextmenu要出现删除功能的节点
            "video": true
        }
    });
    editor.ready(function () {
        Observer.fire('editorCompleted', editor);
        //删除子节点宽度
        if (editor.container) {
            editor.container.style.width = "";
            $(editor.container.childNodes).each(function (i, e) {
                e.style.width = '';
            });
        }
        // 修改默认粘贴
        editor.addListener('beforepaste', function (name, html, root) {
            var root = UE.htmlparser(html.html, true),
                string = UE.filterNode(root, editor.options.filterTxtRules).toHtml();
            if (string) {
                editor.execCommand('insertHtml', string, true);
            }
            editor.fireEvent("afterpaste", html);
            html.html = '';
            return true;
        });
        // 当表格为最后一行时加入p标签
        editor.addListener('contentchange keyup', function () {
            var children = editor.document.body.children,
                autotypeset = editor.getOpt('autotypeset');
            if (children.length && children[children.length - 1].tagName.toLowerCase() === 'table') {
                var p = editor.document.createElement("p");
                p.innerHTML = UE.browser.ie ? '&nbsp;' : '<br />';
                //if (autotypeset.indent) {
                //    p.style.textIndent = autotypeset.indentValue;
                //}
                editor.document.body.appendChild(p);
            }
        });
        // 撤销和恢复后重新调整高度
        editor.addListener('reset', function (type, exclude) {
            editor.autoAdjustHeight();
        });
        editor.addListener('rangechange', function (type, range) {
            Observer.fire('scrollTop', range);
        });

        editor.fireEvent('contentchange');
        // 打开编辑器后保存内容，主要是为了保存上传的文档中的图片样式修改
        Observer.fire("saveContent");
    });

    // ready都执行完后运行
    editor.__allListeners.ready.unshift(function () {
        // 设置图片等资源加载后调整高度
        editor.autoAdjustHeight();
        // 修改table样式
        UE.utils.cssRule('table',
            //选中的td上的样式
            '.selectTdClass{background-color:#edf5fa !important}' +
            'table.noBorderTable td,table.noBorderTable th,table.noBorderTable caption{border:1px dashed #000 !important}' +
            //插入的表格的默认样式
            'table{margin-bottom:10px;border-collapse:collapse;display:table;}' +
            'td,th{padding: 5px 10px;border: 1px solid #000;}' +
            'caption{border:1px dashed #000;border-bottom:0;padding:3px;text-align:center;}' +
            'th{border-top:1px solid #BBB;background-color:#F7F7F7;}' +
            'table tr.firstRow th{border-top-width:2px;}' +
            '.ue-table-interlace-color-single{ background-color: #fcfcfc; } .ue-table-interlace-color-double{ background-color: #f7faff; }' +
            'td p{margin:0;padding:0;}', editor.document);
    });

    editor.addListener('selectionchange', function () {
        Observer.fire('selectionchange', editor);
    });

    editor.addListener('contentchange', function () {
        Observer.fire('contentchange', editor);
    });

    return editor;
}

export {initialContent, initEditor}
